<template>
    <div class="app-container">
        <div class="page">
            <el-card class="page-section" :body-style="{ padding: '8px 15px' }">
<!--                <div class="section-title">积分数据</div>-->
                <el-row class="page-section__body" :gutter="10">
                    <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">

                        <div class="data-card" style="background:#3EB2A6">
                            <div class="data-card__label">已放的积分数</div>
                            <div class="data-card__value">{{communitySocore.alreadyScore}}</div>
                            <svg-icon class-name="data-card__icon" icon-class="redis" />
                        </div>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
                        <div class="data-card" style="background:#F47679">
                            <div class="data-card__label">已消耗积分数</div>
                            <div class="data-card__value">{{communitySocore.consumeScore}}</div>
                            <svg-icon class-name="data-card__icon" icon-class="ysy" />
                        </div>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
                        <div class="data-card" style="background:#F47679">
                            <div class="data-card__label">今日发放积分</div>
                            <div class="data-card__value">{{communitySocore.todayScore}}</div>
                            <svg-icon class-name="data-card__icon" icon-class="ff" />
                        </div>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
<!--                        <data-card-->
<!--                            label="今日消耗积分"-->
<!--                            :value="systemBoardView.scoreView.todayUsedTotal"-->
<!--                            icon="xh"-->
<!--                            background="#D6B4B7"-->
<!--                        />-->
                        <div class="data-card" style="background:#D6B4B7">
                            <div class="data-card__label">今日消耗积分</div>
                            <div class="data-card__value">{{communitySocore.todayConsumeScore}}</div>
                            <svg-icon class-name="data-card__icon" icon-class="xh" />
                        </div>
                    </el-col>

                </el-row>
            </el-card>

        </div>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="70px">
            <el-form-item label="户主身份证:" prop="familyHeadCard" label-width="80">
                <el-input
                    v-model="queryParams.familyHeadCard"
                    placeholder="请输入户主身份证号"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="户主:" prop="familyName" label-width="80">
                <el-input
                    v-model="queryParams.familyName"
                    placeholder="请输入户主名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="成员:" prop="memberName" label-width="80">
                <el-input
                    v-model="queryParams.memberName"
                    placeholder="请输入家庭成员名"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="联系方式:" prop="familyMobile" label-width="80">
                <el-input
                    v-model="queryParams.familyMobile"
                    placeholder="请输入联系方式"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item label="积分状态:" prop="status" label-width="80">
                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                    <el-option
                        v-for="dict in dict.type.score_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="排序规则:" prop="sort" label-width="80">
                <el-radio-group v-model="queryParams.sort">
                    <el-radio label="a.family_id">自然排序</el-radio>
                    <el-radio label="a.family_score">积分高低排序</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                    type="primary"-->
<!--                    plain-->
<!--                    icon="el-icon-plus"-->
<!--                    size="mini"-->
<!--                    @click="handleAdd"-->
<!--                    v-hasPermi="['organization:family:add']"-->
<!--                >新增-->
<!--                </el-button>-->
<!--            </el-col>-->

            <el-col :span="1.5">
                <el-button
                    v-if="queryParams.sort=='a.family_id'"
                    type="primary"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['organization:family:export']"
                >
                    按自然排序导出
                </el-button>
                <el-button
                    v-if="queryParams.sort=='a.family_score'"
                    type="danger"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['organization:family:export']"
                >
                    按积分高低排序导出
                </el-button>


            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="familyList" @selection-change="handleSelectionChange" border>
            <el-table-column label="ID" align="center" prop="familyId" width="100"/>
            <el-table-column label="村社" align="center" prop="communityName" width="100"/>
            <el-table-column label="身份证号" align="center" prop="familyHeadCard" width="200"/>
            <el-table-column label="户主" align="center" prop="familyName"/>
            <el-table-column label="联系方式" align="center" prop="familyMobile">
                <template slot-scope="scope">{{splitPhone(scope.row.familyMobile)}}</template>
            </el-table-column>
            <el-table-column label="家庭积分数" align="center" prop="familyScore">
                <template slot-scope="scope">{{numberWithCommas(scope.row.familyScore)}}</template>
            </el-table-column>
            <el-table-column label="有效期" align="center" width="320" >
                <template slot-scope="scope">
                    {{parseTime(scope.row.expireStart)}} ~ {{parseTime(scope.row.expireEnd)}}
                </template>

            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.score_status" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <!--<el-table-column label="备注" align="center" prop="remark"/>-->
            <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width" fixed="right">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-tickets"
                        @click="handleScoreLog(scope.row)"
                        v-hasPermi="['organization:family:edit']"
                    >查看流水
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-document-add"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['organization:family:edit']"
                    >调整积分
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改家庭积分对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="formScoreLog" :rules="formScoreLogRules" label-width="100px">
                <el-form-item label="户主:" prop="familyScore">{{form.familyName}}</el-form-item>
                <el-form-item label="证件号:" prop="familyScore">{{form.familyHeadCard}}</el-form-item>
                <el-form-item label="当前积分数:" prop="familyScore">{{numberWithCommas(form.familyScore)}}</el-form-item>
                <el-form-item label="交易类型:" prop="orderType">
                    <el-radio-group v-model="formScoreLog.orderType">
                        <el-radio label="7">系统调加</el-radio>
                        <el-radio label="8">系统调减</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="变动积分:" prop="changeScore">
                    <el-input v-model="formScoreLog.changeScore" placeholder="调整积分"/>
                </el-form-item>
                <el-form-item label="变更原因:" prop="remark">
                    <el-input v-model="formScoreLog.remark" type="textarea" placeholder="变更原因"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitScoreLogForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 积分流水 -->
        <el-dialog v-model="peopleScoreLog.queryParams" :visible.sync="peopleScoreLog.visible"  :close-on-click-modal="false"	width="1200px" append-to-body>
            <div slot="title" class="dialog-title">
                <span class="title-text" v-html="peopleScoreLog.title"></span>
                <div class="button-right">
                    <span class="title-close" @click="cancel"></span>
                </div>
            </div>

            <el-table v-loading="peopleScoreLog.loading" highlight-current-row :data="peopleScoreLog.peopleScoreLogList"  border width="100%">
                <el-table-column label="积分明细ID" align="center" prop="id" width="90" />
                <el-table-column label="交易流水号" align="center" prop="orderSerialNo" width="200" :show-overflow-tooltip="true" />
                <el-table-column label="交易类型" align="center" prop="orderType" width="120" :show-overflow-tooltip="true" >
                    <template slot-scope="scope">
                        <span v-if="scope.row.orderType==1">文章阅读</span>
                        <span v-if="scope.row.orderType==2">文章评论</span>
                        <span v-if="scope.row.orderType==3">文章收藏</span>
                        <span v-if="scope.row.orderType==4">其他奖励</span>
                        <span v-if="scope.row.orderType==5">商品兑换</span>
                        <span v-if="scope.row.orderType==6">商品退还</span>
                        <span v-if="scope.row.orderType==7">系统调加</span>
                        <span v-if="scope.row.orderType==8">系统调减</span>
                        <span v-if="scope.row.orderType==10">参与活动</span>
                        <span v-if="scope.row.orderType==11">分享荣誉</span>
                    </template>
                </el-table-column>
                <el-table-column label="支出类型" align="center" prop="orderScoreType" width="100" :show-overflow-tooltip="true" >
                    <template slot-scope="scope">
                        <el-tag type="succ" v-if="scope.row.orderScoreType==1">收入</el-tag>
                        <el-tag type="danger" v-if="scope.row.orderScoreType==2">支出</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="交易积分数" align="center" prop="changeScore" width="100" :show-overflow-tooltip="true" />
                <el-table-column label="交易前积分数" align="center" prop="totalScore" width="130" :show-overflow-tooltip="true" />
                <el-table-column label="描述" align="left" prop="remark" width="300" :show-overflow-tooltip="true" />
                <el-table-column label="交易时间" align="center" prop="createTime" width="150" :show-overflow-tooltip="true" />
            </el-table>

            <pagination v-show="peopleScoreLog.total > 0" :total="peopleScoreLog.total" :page.sync="peopleScoreLog.queryParams.pageNum" :limit.sync="peopleScoreLog.queryParams.pageSize" @pagination="getScoreLogList" />
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogCancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import {listFamily, getFamily, delFamily, addFamily, updateFamily,changeFamilyScore} from "@/api/organization/family";
import {listFamilyScoreLog,communityScore} from "@/api/organization/familyScoreLog";
import {numberWithCommas, splitPhone} from "@/utils/ruoyi";
import DataCard from "@/views/dashboard2/DataCard.vue";
import {background} from "quill/ui/icons";
export default {
    name: "Family",
    computed: {
        background() {
            return background
        }
    },
    components: {DataCard},
    dicts: ['score_status'],
    data() {
        return {
            communitySocore:{
                alreadyScore:0,
                consumeScore:0,
                todayScore:0,
                todayConsumeScore:0,
            },
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 家庭积分表格数据
            familyList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                familyHeadCard: null,
                familyName: null,
                familyMobile: null,
                memberName: null,
                familyScore: null,
                status: null,
                tmp: null,
                sort:'a.family_id'
            },
            // 表单参数
            form: {},
            formScoreLog:{"orderType":"7"},
            // 表单校验
            rules: {
                familyScore: [
                    {required: true, message: "家庭积分数不能为空", trigger: "blur"}
                ],
            },
            formScoreLogRules:{
                orderType: [{required: true, message: "校验类型不能空", trigger: "blur"}],
            },
            peopleScoreLog:{
                visible: false,
                title: '家庭积分流水',
                peopleScoreLogList: [],
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: false,
                // 总条数
                total: 0,
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    peopleId: null,
                    familyId: null,
                },
            },
        };
    },
    activated(){
        if(this.$route.query.s!='') {
            this.queryParams.tmp = this.$route.query.s;
            this.getList();
        }
    },
    created() {
        if(this.$route.query.s!='') {
            this.queryParams.tmp = this.$route.query.s;
        }
        this.$loading();
        this.communitySocoreRequest();
        this.getList();
        this.$loading().close();
    },
    methods: {
        numberWithCommas,
        splitPhone,
        /** 查询家庭积分列表 */
        getList() {
            this.loading = true;
            listFamily(this.queryParams).then(response => {
                this.familyList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /**
         * 社区积分列表
         */
        communitySocoreRequest() {
            communityScore({"id":1200}).then(response => {
                this.communitySocore = response.data;
            });
        },
        handleScoreLog(row) {
            this.peopleScoreLog.queryParams.familyId = row.familyId;
            this.peopleScoreLog.title = "<b><font color='red'>"+row.familyName+'</font></b>家庭积分流水';
            this.getScoreLogList();
        },
        getScoreLogList () {
            this.peopleScoreLog.loading = true;
            // this.peopleScoreLog.queryParams.familyId = familyId;
            listFamilyScoreLog(this.peopleScoreLog.queryParams).then(response => {
                this.peopleScoreLog.peopleScoreLogList = response.rows;
                this.peopleScoreLog.total = response.total;
                this.peopleScoreLog.loading = false;
                this.peopleScoreLog.visible = true;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                familyId: null,
                familyHeadCard: null,
                familyName: null,
                familyMobile: null,
                familyScore: null,
                status: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null,
                tmp: null
            };

            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.queryParams.tmp=null
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams.tmp=null
            const currentRoute = this.$route;
            this.$router.push({ path: currentRoute.path, query: {} });
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.familyId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const familyId = row.familyId || this.ids
            getFamily(familyId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "调整家庭积分";
            });
            this.formScoreLog.familyId = familyId;
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.familyId != null) {
                        updateFamily(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addFamily(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        // 积分变更提交
        submitScoreLogForm(){
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.formScoreLog.familyId != null) {

                        if (this.formScoreLog.changeScore<=0 || isNaN(this.formScoreLog.changeScore) ) {
                            this.$modal.alertError("调整积分数值不能小于等于0");
                            return false;
                        }
                        if (this.formScoreLog.remark==="" || this.formScoreLog.remark===null || this.formScoreLog.remark===undefined || this.formScoreLog.remark.trim()==="") {
                            this.$modal.alertError("调整原因不能空");
                            return false;
                        }
                        //this.formScoreLog.orderType = parseInt(this.formScoreLog.orderType)
                        this.formScoreLog.changeScore = parseInt(this.formScoreLog.changeScore.trim())
                        this.formScoreLog.remark =  this.formScoreLog.remark.trim()
                        this.$loading("积分设置中...");
                        changeFamilyScore(this.formScoreLog).then(response => {
                            this.$modal.msgSuccess("调整积分成功");
                            this.open = false;
                            this.getList();
                            this.$loading().close();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const familyIds = row.familyId || this.ids;
            this.$modal.confirm('是否确认删除家庭积分编号为"' + familyIds + '"的数据项？').then(function () {
                return delFamily(familyIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            //this.queryParams.pageSize = 100;
            let fileName = "";
            if (this.queryParams.sort=='a.family_score') {
                fileName = "家庭积分高低排名_"+this.getFormattedDate()+".xlsx";
            }
            else {
                fileName = "按自然排序"+this.getFormattedDate()+".xlsx";
            }
            this.download('organization/family/export', {
                ...this.queryParams
            }, fileName) //`family_${new Date().getTime()}.xlsx`
        },
        getFormattedDate() {
            const now = new Date();
            const year = now.getFullYear();
            const month = String(now.getMonth() + 1).padStart(2, '0');
            const day = String(now.getDate()).padStart(2, '0');
            const hours = String(now.getHours()).padStart(2, '0');
            const minutes = String(now.getMinutes()).padStart(2, '0');
            return `${year}${month}${day}${hours}${minutes}`;
        },
        clickDialog (row) {
            // const peopleId = row.peopleId;
            this.peopleScoreLog.queryParams.pageNum = 1;
            this.peopleScoreLog.queryParams.peopleId = row.peopleId;
            this.getScoreLogList()
            this.peopleScoreLog.visible = true;
        },
        dialogCancel() {
            this.peopleScoreLog.visible = false;
        }
    }
};
</script>

<style lang="scss" scoped>
.page {
    //padding: 12px 16px;
    padding-bottom: 10px;
}

.page-section {
    margin-bottom: 12px;

    &__body {
        margin-top: 10px;
    }

    &:last-child {
        margin-bottom: 0;
    }
}

.section-title {
    font-size: 14px;
    line-height: 35px;

    > span {
        line-height: 20px;
        vertical-align: middle;
    }

    &::before {
        content: '';
        display: inline-block;
        width: 6px;
        height: 20px;
        background-color: #2ad199;
        margin-right: 10px;
        vertical-align: middle;
    }
}
.data-card {
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 100%;
    height: 100px;
    padding: 0 24px;
    border-radius: 6px;
    color: #fff;
    margin-bottom: 10px;

    &__label {
        font-size: 14px;
    }

    &__value {
        font-size: 24px;
        margin-top: 12px;
    }

    &__icon {
        position: absolute;
        top: 0;
        bottom: 0;
        right: 24px;
        width: 48px;
        height: 48px;
        margin: auto 0;
        fill: currentColor;
        opacity: 0.6;
    }
}
</style>
